namespace RSCore.Files;
public sealed record BuildFile(string Name = "Build") : RSFile(Name)
{
	/// <summary>
	/// 汉化包版本
	/// </summary>
	public Version Version { get; set; } = new Version();

	/// <summary>
	/// 要翻译的语言
	/// </summary>
	public string Language { get; set; } = "en-us";

	/// <summary>
	/// 作者名
	/// </summary>
	public string Author { get; set; } = string.Empty;

	/// <summary>
	/// 汉化包描述
	/// </summary>
	public string Description { get; set; } = string.Empty;

	/// <summary>
	/// 汉化Mod名
	/// </summary>
	public string ModName { get; set; } = string.Empty;
	
	/// <summary>
	/// 汉化Mod版本
	/// </summary>
	public Version ModVersion { get; set; } = new Version();

	public override void Deserialize(JsonValue json)
	{
		Version = Version.Parse(json[nameof(Version)]);
		Language = json[nameof(Language)];
		Author = json[nameof(Author)];
		Description = json[nameof(Description)];
		ModName = json[nameof(ModName)];
		ModVersion = Version.Parse(json[nameof(ModVersion)]);
	}

	public override void Load(BinaryReader reader)
	{
		Version = Version.Parse(reader.ReadString());
		Language = reader.ReadString();
		Author = reader.ReadString();
		Description = reader.ReadString();
		ModName = reader.ReadString();
		ModVersion = Version.Parse(reader.ReadString());
	}

	public override void Read(StreamReader reader)
	{
		var properties = reader.ReadProperties();
		Version = Version.Parse(properties.GetText(nameof(Version)));
		Language = properties.GetText(nameof(Language));
		Author = properties.GetText(nameof(Author));
		Description = properties.GetText(nameof(Description));
		ModName = properties.GetText(nameof(ModName));
		ModVersion = Version.Parse(properties.GetText(nameof(ModVersion)));
	}

	public override void Save(BinaryWriter writer)
	{
		writer.Write(Version.ToString());
		writer.Write(Language);
		writer.Write(Author);
		writer.Write(Description);
		writer.Write(ModName);
		writer.Write(ModVersion.ToString());
	}

	public override JsonValue Serialize()
	{
		return new JsonObject
		{
			{ nameof(Version), Version.ToString() },
			{ nameof(Language), Language },
			{ nameof(Author), Author },
			{ nameof(Description), Description },
			{ nameof(ModName), ModName },
			{ nameof(ModVersion), ModVersion.ToString() },
		};
	}

	public override void Write(StreamWriter writer, RSFile? annotation = null)
	{
		writer.WriteProperty(Version.ToString(), null, nameof(Version));
		writer.WriteProperty(Language);
		writer.WriteProperty(Author);
		writer.WriteProperty(Description);
		writer.WriteProperty(ModName);
		writer.WriteProperty(ModVersion.ToString(), null, nameof(ModVersion));
	}
}